package com.nervenets.web.controller.handler.admin.user;

import com.alibaba.fastjson.JSONArray;
import com.nervenets.web.controller.handler.SessionHandler;
import com.nervenets.web.controller.handler.user.wealth.InvoiceFlow;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.enumeration.*;
import com.nervenets.web.hibernate.domain.user.RewardInvoice;
import com.nervenets.web.hibernate.domain.user.WealthFlow;
import com.nervenets.web.rabbitmq.FlowMessage;
import com.nervenets.web.redis.utils.ISyncExecutor;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class UserRewardInvoiceCheckHandler extends SessionHandler<UserRewardInvoiceCheckParams> {
    @Override
    public ArrayList<Role> allowRoles() {
        return Role.all();
    }

    @Override
    public boolean needLog() {
        return false;
    }

    @Override
    public String handle(UserRewardInvoiceCheckParams params, HttpServletRequest request) throws Exception {
        return (String) redisSyncLocker.idLock(params.getInvoiceId(), o -> {
            final RewardInvoice invoice = userDao.findById(params.getInvoiceId(), RewardInvoice.class);
            if (null == invoice) return errorMessage(MessageCode.code_404, "发票信息不存在");
            if (!AuthStatus.checking.equals(invoice.getStatus()))
                return errorMessage(MessageCode.code_444, "该记录已处理");

            if (params.isSuccess()) {
                invoice.setStatus(AuthStatus.success);
                userDao.saveOrUpdate(invoice);

                final List<InvoiceFlow> flowIds = JSONArray.parseArray(invoice.getFlows(), InvoiceFlow.class);
                for (InvoiceFlow invoiceFlow : flowIds) {
                    final WealthFlow flow = userDao.findById(invoiceFlow.getFlowId(), WealthFlow.class);
                    flow.setFlowStatus(FlowStatus.success);
                    userDao.saveOrUpdate(flow);

                    messageProducer.flow(FlowMessage.wealthChange(invoice.getUserId(), getIp(request), UnionType.flow, flow.getUnionId(), Math.abs(flow.getOffset()), OperateType.rewardWithdraw, PayType.surplus, FlowStatus.success, "奖励金提现发票保证金退还"));
                }

            } else {
                invoice.setStatus(AuthStatus.failed);
                invoice.setFailedReason(params.getFailedReason());
                userDao.saveOrUpdate(invoice);

                final List<InvoiceFlow> flowIds = JSONArray.parseArray(invoice.getFlows(), InvoiceFlow.class);
                for (InvoiceFlow invoiceFlow : flowIds) {
                    final WealthFlow flow = userDao.findById(invoiceFlow.getFlowId(), WealthFlow.class);
                    flow.setFlowStatus(FlowStatus.handling);
                    userDao.saveOrUpdate(flow);
                }
            }

            return successMessage();
        });
    }

    @Override
    public String result() throws Exception {
        return successMessage();
    }
}
